7 Star 6 Fork 1

佰钧成开源官方组织 / barcodescanner

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Introduction

HarmonyOS library projects that provides easy to use and extensible Barcode Scanner views based on ZXing and ZBar.

Screenshots

main_ability scan_result

ZXing

Installation

Add the following dependency to your build.gradle file.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
    implementation 'com.google.zxing:core:3.4.0'
    compile project(path: ':zxing')
    compile project(path: ':core')
}

Simple Usage

1.) Add camera permission to your config.json file:

"reqPermissions": [
  {
    "name": "ohos.permission.CAMERA",
    "reason": "open camera",
    "usedScene":
    {
      "ability": ["me.dm7.barcodescanner.zxing.sample.MainAbility",],
      "when": "always"
    }
  }
]

2.) A very basic ability would look like this:

public class SimpleScannerAbility extends Ability implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;

    @Override
    public void onStart(Bundle state) {
        super.onStart(state);
        // Dynamiclly add ZXingScannerView to content_frame which represents a StackLayout
        ComponentContainer contentFrame = (ComponentContainer) findComponentById(ResourceTable.Id_content_frame);
        mScannerView = new ZXingScannerView(this);
        contentFrame.addComponent(mScannerView);
    }

    @Override
    public void onActive() {
        super.onActive();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on active
    }

    @Override
    public void onInActive() {
        super.onInActive();
        mScannerView.stopCamera();           // Stop camera on inactive
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        HiLog.info(LOG_LABEL, "handleResult," + rawResult.getText());// Prints scan results
        HiLog.info(LOG_LABEL, "handleResult," + rawResult.getBarcodeFormat().toString());// Prints the scan format (qrcode, pdf417 etc.)

        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

Please take a look at the zxing-sample project for a full working example.

Advanced Usage

Take a look at the FullScannerAbility.java or FullScannerFraction.java classes to get an idea on advanced usage.

Interesting methods on the ZXingScannerView include:

// Toggle flash:
void setFlash(boolean);

// Toogle autofocus:
void setAutoFocus(boolean);

// Specify interested barcode formats:
void setFormats(List<BarcodeFormat> formats);

// Specify the cameraId to start with:
void startCamera(int cameraId);

Specify front-facing or rear-facing cameras by using the void startCamera(int cameraId); method.

Supported Formats:

BarcodeFormat.UPC_A
BarcodeFormat.UPC_E
BarcodeFormat.EAN_13
BarcodeFormat.EAN_8
BarcodeFormat.RSS_14
BarcodeFormat.CODE_39
BarcodeFormat.CODE_93
BarcodeFormat.CODE_128
BarcodeFormat.ITF
BarcodeFormat.CODABAR
BarcodeFormat.QR_CODE
BarcodeFormat.DATA_MATRIX
BarcodeFormat.PDF_417

ZBar

Installation

Add the following dependency to your build.gradle file.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.har', '*.so'])
    testCompile'junit:junit:4.12'
    compile project(path: ':core')
}

Simple Usage

1.) Add camera permission to your config.json file:

"reqPermissions": [
  {
    "name": "ohos.permission.CAMERA",
    "reason": "open camera",
    "usedScene":
    {
      "ability": ["me.dm7.barcodescanner.zbar.sample.MainAbility",],
      "when": "always"
    }
  }
]

2.) A very basic activity would look like this:

public class SimpleScannerAbility extends Ability implements ZbarScannerView.ResultHandler {
    private ZbarScannerView mScannerView;

    @Override
    public void onStart(Bundle state) {
        super.onStart(state);
        ComponentContainer contentFrame = (ComponentContainer) findComponentById(ResourceTable.Id_content_frame);
        mScannerView = new ZbarScannerView(this);
        contentFrame.addComponent(mScannerView);
    }

    @Override
    public void onActive() {
        super.onActive();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on active
    }

    @Override
    public void onInActive() {
        super.onInActive();
        mScannerView.stopCamera();           // Stop camera on inactive
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        HiLog.info(LOG_LABEL, "handleResult," + rawResult.getText());// Prints scan results
        HiLog.info(LOG_LABEL, "handleResult," + rawResult.getBarcodeFormat().toString());// Prints the scan format (qrcode, pdf417 etc.)

        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

Please take a look at the zbar-sample project for a full working example.

Advanced Usage

Take a look at the FullScannerAbility.java or FullScannerFraction.java classes to get an idea on advanced usage.

Interesting methods on the ZBarScannerView include:

// Toggle flash:
void setFlash(boolean);

// Toogle autofocus:
void setAutoFocus(boolean);

// Specify interested barcode formats:
void setFormats(List<BarcodeFormat> formats);

Specify front-facing or rear-facing cameras by using the void startCamera(int cameraId); method.

Supported Formats:

BarcodeFormat.PARTIAL
BarcodeFormat.EAN8
BarcodeFormat.UPCE
BarcodeFormat.ISBN10
BarcodeFormat.UPCA
BarcodeFormat.EAN13
BarcodeFormat.ISBN13
BarcodeFormat.I25
BarcodeFormat.DATABAR
BarcodeFormat.DATABAR_EXP
BarcodeFormat.CODABAR
BarcodeFormat.CODE39
BarcodeFormat.PDF417
BarcodeFormat.QR_CODE
BarcodeFormat.CODE93
BarcodeFormat.CODE128

Include ZBar Libraries

zbar-so

.so library is not well supported on current HarmonyOS version, so we can't include zbar dependency by har/jar, instead we need to import zbar so and jar to entry's libs folder as above.

Credits

Almost all of the code for these library projects is based on:

  1. Camera Preview api from HarmonyOS SDK Documents
  2. The Original barcodescanner project:https://github.com/dm77/barcodescanner
  3. The ZXing project: https://github.com/zxing/zxing
  4. The ZBar SDK: https://github.com/ZBar/ZBar/tree/master/android (Previously: http://sourceforge.net/projects/zbar/files/AndroidSDK/)

License

License for code written in this project is: Apache License, Version 2.0

License for zxing and zbar projects is here:

Copyright (c) 2014 Dushyanth Maguluru Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

基于zxing和zbar提供易于使用的二维码扫描功能 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/baijuncheng-open-source/barcodescanner.git
git@gitee.com:baijuncheng-open-source/barcodescanner.git
baijuncheng-open-source
barcodescanner
barcodescanner
master

搜索帮助